<?php
/**
 * Created by zhang.
 * User: zhang
 * Date: 2018/7/26
 * Time: 23:01
 */

namespace app\index\controller;


class Coupon  extends Base
{

    public function getCouponInfo($id){
        $where['coupon_id'] = $id;
        $result = (new \app\index\model\coupon())->getCouponInfo($where);
        $this ->view->couponInfo = $result;
        return $this ->view ->fetch();
    }


    public function buyCouponInfo(){
        $couponid = input('post.couponid');
        $where['coupon_id'] = $couponid;
        $result = (new \app\index\model\coupon())->getCouponInfo($where);
        if($result['coupon_stat'] =2){
            echo json_encode(['code'=>0,'msg'=>"优惠券已下架"]);
            exit;
        }

        if($result['coupon_use_end_time'] < date("y-m-d H:i:s",time())){
            echo json_encode(['code'=>0,'msg'=>"优惠券使用时间不再范围内"]);
            exit;
        }
        if($result['coupon_number'] - $result['coupon_sale_num'] <=0){
            echo json_encode(['code'=>0,'msg'=>"优惠券已卖完"]);
            exit;
        }

        if($result['coupon_surplus_num'] <=0){
            echo json_encode(['code'=>0,'msg'=>"优惠券已卖完"]);
            exit;
        }
        var_dump($result);exit;
    }

}